package ywpt.web.action.config;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import com.opensymphony.xwork2.ModelDriven;
import com.opensymphony.xwork2.Preparable;

import ywpt.components.acegi.domain.Operation;
import ywpt.core.Constants;
import ywpt.core.SystemMessage;
import ywpt.core.exception.UniqueConstraintException;
import ywpt.core.web.BaseAction;
import ywpt.model.ProjectInfo;
import ywpt.model.ProjectPeople;
import ywpt.service.config.ProjectInfoService;
import ywpt.service.config.ProjectPeopleService;
import ywpt.web.common.OperationConstants;
import ywpt.web.common.PublicFunction;

public class ProjectPeopleAction extends BaseAction implements ModelDriven<ProjectPeople>,Preparable{

	/**
	 * 项目成员
	 */
	private static final long serialVersionUID = -2027007567685281244L;
	private boolean[] popedom=new boolean[Constants.POPEDOM_LEN];
	private Long id;
	private ProjectPeople projectPeople;
	private List<ProjectPeople> projectPeopleList = new ArrayList<ProjectPeople>();
	private ProjectPeopleService ProjectPeopleService;
	private ProjectInfoService projectInfoService;
	private ProjectInfo projectInfo;
	private Long projectId;
	private Long postId;
	private String[] peoId;
	
	public String[] getPeoId() {
		return peoId;
	}

	public void setPeoId(String[] peoId) {
		this.peoId = peoId;
	}

	public Long getPostId() {
		return postId;
	}

	public void setPostId(Long postId) {
		this.postId = postId;
	}

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public ProjectPeople getProjectPeople() {
		return projectPeople;
	}

	public void setProjectPeople(ProjectPeople projectPeople) {
		this.projectPeople = projectPeople;
	}

	public List<ProjectPeople> getProjectPeopleList() {
		return projectPeopleList;
	}

	public void setProjectPeopleList(List<ProjectPeople> projectPeopleList) {
		this.projectPeopleList = projectPeopleList;
	}

	public ProjectPeopleService getProjectPeopleService() {
		return ProjectPeopleService;
	}

	public void setProjectPeopleService(ProjectPeopleService projectPeopleService) {
		ProjectPeopleService = projectPeopleService;
	}

	public Long getProjectId() {
		return projectId;
	}

	public void setProjectId(Long projectId) {
		this.projectId = projectId;
	}
	public boolean[] getPopedom() {
		return popedom;
	}

	public void setPopedom(boolean[] popedom) {
		this.popedom = popedom;
	}
	//--------------------------------------------------------------------------------------------------
	@Override
	public ProjectPeople getModel() {
		return this.projectPeople;
	}

	@Override
	public void prepare() throws Exception {
		if(id != null){
			this.projectPeople = ProjectPeopleService.getProjectPeopleById(id);
		}else{
			this.projectPeople = new ProjectPeople();
		}
		HashMap<String,Operation> oper=this.getOperMapByUser(this.getCurrentUser());
		PublicFunction.getOperationPopedom(oper,popedom,OperationConstants.CONFIG_PROJECT);
	}
	
	protected void showInfo(){
		if (this.projectPeople.getProject()!=null) {
			this.projectId = this.projectPeople.getProject().getId();
		}
		if (this.projectPeople.getPost()!=null) {
			this.postId = this.projectPeople.getPost().getId();
		}
	}
//===================================================================================================
	
	public String input() {
		return INPUT;
	}
	public String edit() {
		if (id==null) {
			this.addActionMessage(SystemMessage.NO_OBJECT);
		}else {
			this.projectPeople = this.ProjectPeopleService.getProjectPeopleById(id);
			showInfo();
		}
		return INPUT;
	}
	
	public String save() {
		try {
			projectInfo = projectInfoService.getProjectInfoById(projectId);
			if (projectInfo.getResponser()!=null&&projectInfo.getResponser().getId().equals(this.getCurrentUser().getPeople().getId())) {
				if (id==null) {
					projectPeople.setCreater(this.getCurrentUser());
					projectPeople.setCreateDate(new Date());
					ProjectPeopleService.saveProjectPeople(projectPeople,projectId,postId,peoId);
					this.addActionMessage(SystemMessage.SAVE_SUCCESS);
				} else {
					projectPeople.setModifyer(this.getCurrentUser());
					projectPeople.setModifyDate(new Date());
					ProjectPeopleService.updateProjectPeople(projectPeople,postId,peoId);
					this.addActionMessage(SystemMessage.UPDATE_SUCCESS);
				}				
			} else {
				this.addActionMessage("你不是该项目的负责人，不能进行信息的更新！");
			}
			return SUCCESS;			
		} catch (UniqueConstraintException e) {
			this.addActionMessage(SystemMessage.SAVE_FAIL+e.getMessage());
			return INPUT;	
		}

	}
	
	public String delete() {
		if (id==null) {
			this.addActionMessage(SystemMessage.NO_OBJECT);
			return INPUT;
		} else {
			projectInfo = this.ProjectPeopleService.getProjectPeopleById(id).getProject();
			if (projectInfo.getResponser()!=null&&projectInfo.getResponser().getId().equals(this.getCurrentUser().getPeople().getId())) {
				this.ProjectPeopleService.removeProjectPeople(id);
				return SUCCESS;
			} else {
				this.addActionMessage("你不是该项目的负责人，不能进行信息的更新！");
				return INPUT;
			}
		}
	}

	public ProjectInfoService getProjectInfoService() {
		return projectInfoService;
	}

	public void setProjectInfoService(ProjectInfoService projectInfoService) {
		this.projectInfoService = projectInfoService;
	}

	public ProjectInfo getProjectInfo() {
		return projectInfo;
	}

	public void setProjectInfo(ProjectInfo projectInfo) {
		this.projectInfo = projectInfo;
	}


	
}
